#include <func.h>

int main(int argc, char **argv)
{
    ARGS_CHECK(argc, 3);

    // 1. 生成套接字标识符
    int sfd = socket(AF_INET, SOCK_DGRAM, 0);

    // 2. bind
    struct sockaddr_in ser_addr;
    bzero(&ser_addr, sizeof(ser_addr));
    ser_addr.sin_family = AF_INET;
    ser_addr.sin_port = htons(atoi(argv[2]));
    ser_addr.sin_addr.s_addr = inet_addr(argv[1]);
    int ret = bind(sfd, (struct sockaddr *)&ser_addr, sizeof(ser_addr));
    ERROR_CHECK(ret, -1, "bind");

    // 3. recvfrom
    char buf[128] = {0};
    struct sockaddr_in client_addr;
    bzero(&client_addr, sizeof(client_addr));
    int fromlen = sizeof(client_addr);
    ret = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr *)&(client_addr), &fromlen);
    printf("I am server, gets = %s\n", buf);

    return 0;
}